/**
 * 拒绝面试原因
 */
import React from 'react';
import { TextareaItem, Toast } from 'antd-mobile';
import { router, withRouter } from 'umi';
import { createForm } from 'rc-form';
import { PageWrap, EditListItem, Button, Picker } from './components';
import { refuseInterview } from '../service';

const list = [
    {
        label: '已找到新工作',
        value: 1
    },
    {
        label: '留在原公司',
        value: 2
    },
    {
        label: '办公地点较远',
        value: 3
    },
    {
        label: '其他',
        value: 99
    }
];

const Refuse = ({ location, form }) => {
    const { query } = location;
    const { getFieldProps } = form;
    const [submitLoading, setSubmitLoading] = React.useState(false);

    const handleSubmit = () => {
        form.validateFields(async (err, values) => {
            // console.log('handleSubmit -> values', values);
            if (!err) {
                const { reasonCode, remark } = values;
                if (reasonCode?.[0] === 99 && !remark) {
                    Toast.info('请输入备注');
                    return;
                }
                // console.log(JSON.stringify(params));
                setSubmitLoading(true);
                const { code, msg } = await refuseInterview(
                    {
                        reasonCode: reasonCode?.[0],
                        remark
                    },
                    query.nid
                );
                setSubmitLoading(false);
                if (code === 10000) {
                    history.goBack();
                } else {
                    Toast.info(msg);
                }
                return;
            }
            Toast.info('请选择原因');
        });
    };

    const required = form.getFieldValue('reasonCode')?.[0] === 99;

    return (
        <PageWrap
            title="拒绝面试"
            footer={
                <Button type="primary" loading={submitLoading} onClick={handleSubmit}>
                    提交
                </Button>
            }
        >
            <div style={{ padding: '0 16px' }}>
                <Picker
                    title="原因"
                    data={list}
                    {...getFieldProps('reasonCode', { rules: [{ required: true, message: '请选择原因' }] })}
                >
                    <EditListItem title="原因">原因</EditListItem>
                </Picker>
                <EditListItem title="备注" required={required} showArrow={false}>
                    <TextareaItem
                        {...getFieldProps('remark', {
                            initialValue: ''
                        })}
                        rows={5}
                        count={4000}
                        placeholder="请输入备注"
                    />
                </EditListItem>
            </div>
        </PageWrap>
    );
};

export default withRouter(createForm()(Refuse));
